package com.bdsy.business.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.bdsy.api.annotation.IgnoreAuth;
import com.bdsy.business.model.OrderDetails;
import com.bdsy.business.model.ShopCar;
import com.bdsy.business.service.ShopCarService;
import com.bdsy.common.annotation.Json;
import com.bdsy.common.utils.DateRandomUtil;
import com.bdsy.common.utils.Result;
import com.bdsy.system.annotation.SysLog;

/**
 * 购物车
 * 
 * @author MING
 * @date 2018年1月12日 下午12:03:01
 */

@Controller
@RequestMapping(value = { "${web.adminPath}", "${web.appPath}" })
public class ShopCarController {

	@Autowired
	ShopCarService shopCarService;

	/**
	 * 查询购物车信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "selectShopCarById")
	public Object selectShopCarById(String userId) {
		Map<String, Object> map = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> shopCarList = shopCarService.selectShopCarById(userId);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", shopCarList);
		return map;
	}

	/**
	 * 查询租赁购物车信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "selectZlShopCarById")
	public Object selectZlShopCarById(String userId) {
		Map<String, Object> map = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> zlShopCarList = shopCarService.selectZlShopCarById(userId);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", zlShopCarList);
		return map;
	}

	
	/**
	 * 添加购物车信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "addShopCar")
	public Object addShopCar(ShopCar shopCar) {
		List<ShopCar> shopCa = shopCarService.selectShopCarbyParam(shopCar.getUserId(),shopCar.getShopId());
		
		if(shopCa == null || shopCa.size() == 0){
			boolean result = shopCarService.insert(shopCar);
			if (result) {
				return Result.ok("addSuccess");
			} else {
				return Result.error("addFail");
			}
		}else{
			return Result.error("该艺术品已存在");
		}
	}
	
	/**
	 * 删除购物车信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "deleteShopCar")
	public Object deleteShopCar(String ids) {
		List<String> list = JSON.parseArray(ids, String.class);
		boolean result = shopCarService.deleteShopCar(list);
		if (result) {
			return Result.ok("deleteSuccess");
		} else {
			return Result.error("deleteFail");
		}
	}

	
	/**
	 * 根据用户ID 商品ID 查询购物车信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "shopCarbyParam")
	public Object shopCarbyParam(String userId,int shopId) {
		Map<String, Object> map = new LinkedHashMap<>();
		List<ShopCar> shopCar = shopCarService.selectShopCarbyParam(userId, shopId);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", shopCar);
		return map;
	}

}
